<template>
  <div style="text-align: left; background-color: #f6f7f9; height:670px;">
    <!-- 顶部导航 -->
    <van-sticky>
      <van-nav-bar title="修改地址" left-arrow @click-left="$router.go(-1)">
      </van-nav-bar>
    </van-sticky>
    <van-address-edit
        :area-list="areaList"
        :address-info="addressInfo"
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
    />
  </div>

</template>
<script>
import {areaList} from '@vant/area-data'
export default {
  name: "AddressUpdate",
  data() {
    return {
      areaList,
      searchResult: [],
      addressInfo:{}
    };
  },
  methods: {
    onDelete(){
      let addressId = localStorage.getItem('addressId')
      let url = 'http://localhost:9080/addresses/'+addressId+'/delete';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 20000) {
          this.$toast.success('删除地址成功!');
          this.$router.replace("/user/address");
        }else{
          this.$toast.fail(responseBody.message);
        }
      });
    },
    onSave(content) {
      console.log("addressDetails", content);
      let addressId = localStorage.getItem('addressId')
      let url = 'http://localhost:9080/addresses/'+addressId+'/update';
      let addressDetails = this.qs.stringify(content, {arrayFormat: 'repeat'});
      console.log('addressDetails = ' + addressDetails);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, addressDetails).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 20000) {
          this.$toast.success('修改地址成功!');
          this.$router.replace("/user/address");
        }else{
          this.$toast.fail(responseBody.message);
        }
      });
    },
    loadAddressInfo(){
      let addressId = localStorage.getItem('addressId')
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/addresses/'+addressId+'/details';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        let addressDetails = response.data.data;
        this.addressInfo=addressDetails;
        console.log("addressInfo_>>>>>>>>",this.addressInfo)
      })
    },
  },
  mounted() {
    this.loadAddressInfo()
  }
}
</script>

<style scoped>

</style>
